import { DeleteModal } from '@/components/Modals';
import api from '@/pages/MerchantAccount/MerchantUser/UserManagement/request';
import { createFilterColumns, createTableRequestParams } from '@/utils/table';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { Button, Space, Typography } from 'antd';
import { useMemo, useRef, useState } from 'react';
import BindForm from './BindForm';
import Form from './Form';

function MerchantAccountUserManagement() {
  const actionRef = useRef<ActionType>();

  const [isShowModal, setIsShowModal] = useState(false);
  const [curData, setCurData] = useState<Partial<MerchantAccountUserManagementApi.ListResponse>>(
    {},
  );
  const [filterList, setFilterList] = useState<Requestor.FilterResponse[]>([]);

  // request
  const getFilterList = async () => {
    // const filterList = await api.getFilterList();
    const filterList: Requestor.FilterResponse[] = [];
    setFilterList(filterList);
    return filterList;
  };
  const getListRequest = (
    params: Requestor.TableParams,
    filterList: Requestor.FilterResponse[],
  ) => {
    const requestParams = createTableRequestParams(params, filterList);
    return api.getList({ ...requestParams });
  };
  const getList = async (params: Requestor.TableParams) => {
    if (!filterList || !filterList.length) {
      const filterList = await getFilterList();
      return getListRequest(params, filterList);
    } else {
      return getListRequest(params, filterList);
    }
  };

  // method
  const setState = (state: boolean, data?: MerchantAccountUserManagementApi.ListResponse) => {
    setCurData(data || {});
    setIsShowModal(state);
  };
  const removeRequest = async (params: MerchantAccountUserManagementApi.DeleteParams) => {
    const data = await api.remove(params);
    if (data.success) {
      actionRef.current?.reload();
      setState(false);
    }
  };

  const onConfirmRemove = () => {
    const { id = '' } = curData;
    removeRequest([id]);
  };
  const onCancelRemove = () => {
    setState(false);
  };
  const columns = useMemo(() => {
    const filterColumns =
      createFilterColumns<MerchantAccountUserManagementApi.ListResponse>(filterList);
    const tableColumns: ProColumns<MerchantAccountUserManagementApi.ListResponse>[] = [
      {
        title: '用户名称',
        dataIndex: 'username',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '门店名称',
        dataIndex: 'shopName',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '联系电话',
        dataIndex: 'contactPhone',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '状态',
        dataIndex: 'statusName',
        hideInSearch: true,
        ellipsis: true,
        align: 'center',
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '操作',
        hideInSearch: true,
        align: 'center',
        width: '20%',
        render(_, record) {
          return (
            <Space>
              <Typography.Link>
                <BindForm data={record} onSuccess={() => actionRef.current?.reload()} />
              </Typography.Link>
              <Typography.Link>
                <Form isEdit data={record} onSuccess={() => actionRef.current?.reload()} />
              </Typography.Link>
              <Typography.Link>
                <Button danger type="link" onClick={() => setState(true, record)}>
                  删除
                </Button>
              </Typography.Link>
            </Space>
          );
        },
      },
    ];
    return filterColumns.concat(tableColumns);
  }, [filterList]);
  return (
    <PageContainer>
      <ProTable
        bordered
        size="small"
        rowKey="id"
        options={false}
        actionRef={actionRef}
        pagination={{ defaultPageSize: 10, showSizeChanger: true }}
        toolBarRender={() => [<Form key="reload" onSuccess={() => actionRef.current?.reload()} />]}
        search={{
          optionRender: () => {
            return [
              <Button key="search" type="primary" onClick={() => actionRef.current?.reload()}>
                查询
              </Button>,
            ];
          },
        }}
        columns={columns}
        request={getList}
      />
      <DeleteModal
        open={isShowModal}
        content="确认删除吗？"
        onOk={onConfirmRemove}
        onCancel={onCancelRemove}
      />
    </PageContainer>
  );
}

export default MerchantAccountUserManagement;
